package io.tij;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import util.Constants;

import static util.Print.*;


public class SerialCtl implements Serializable{

	private String a;
	private transient String b;
	
	public SerialCtl(String a, String b) {
		this.a = "Not transient: " + a;
		this.b = "Transient: " + b;
	}
	
	public String toString(){
		return a + "\n" + b;
	}

	private void writeObject(ObjectOutputStream out) throws IOException{
		out.defaultWriteObject();
		out.writeObject(b);
	}
	
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
		in.defaultReadObject();
		b = (String) in.readObject();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException{
		final String filepath = Constants.IO_FILE_BASE_PATH + SerialCtl.class.getName() + ".out";
		SerialCtl serialCtl = new SerialCtl("Test1", "Test2");
		print(serialCtl);
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filepath));
		out.writeObject(serialCtl);
		out.close();
		
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(filepath));
		serialCtl = (SerialCtl)in.readObject();
		in.close();
		print(serialCtl);
	}

}
